package com.chain.fmart

import android.os.Bundle
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import android.util.Log


import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter



import io.flutter.plugin.common.MethodCall;
import android.content.pm.PackageManager;

class MainActivity : FlutterActivity() {
//    private val CHANNEL = "com.um.club/platform"
    private val CHANNEL = "com.chain.fmart/platform"
    private lateinit var broadcastReceiver: BroadcastReceiver

    private lateinit var methodChannel: MethodChannel
    private lateinit var flutterEngine: FlutterEngine

/*    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        this.flutterEngine = flutterEngine
        methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
        methodChannel.setMethodCallHandler { call, result ->
            when (call.method) {
                "startCocosActivity" -> {

                }

                else -> {
                    result.notImplemented()
                }
            }
        }
    }*/


    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        this.flutterEngine = flutterEngine
        methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
        methodChannel.setMethodCallHandler { call, result ->
            when (call.method) {
                "isAppInstalled" -> {
                    val packageName: String? = call.arguments()
                    val pm = packageManager
                    try {
                        pm.getPackageInfo(packageName?: "默认值", PackageManager.GET_ACTIVITIES)
                        result.success(true) // 应用已安装
                    } catch (e: PackageManager.NameNotFoundException) {
                        result.success(false) // 应用未安装
                    }
                }

                else -> {
                    result.notImplemented()
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 注册广播接收器
/*        val filter = IntentFilter("com.um.club.MessageEvent")
        broadcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                val message = intent?.getStringExtra("message")
                if (message == "getToken") {
                    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
                        .invokeMethod("getToken", message, object : MethodChannel.Result {
                            override fun success(result: Any?) {
                                // TODO: 调用 Dart，获取 token
                                val intent = Intent()
                                intent.setAction("com.um.club.CocosCallback")
                                intent.putExtra("token", result.toString())
                                sendBroadcast(intent)
                            }

                            override fun error(
                                errorCode: String,
                                errorMessage: String?,
                                errorDetails: Any?
                            ) {
                                Log.e("Native", "Error: $errorCode, $errorMessage")
                            }

                            override fun notImplemented() {
                                Log.w("Native", "Method not implemented")
                            }
                        })
                }
            }

        }
        registerReceiver(broadcastReceiver, filter)*/

/*        MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler { call, result ->
            // 检查是否安装了特定的应用
            if (call.method.equals("isAppInstalled")) {
                val packageName: String = call.arguments()
                val pm: PackageManager = getPackageManager()
                try {
                    pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
                    result.success(true) // 应用已安装
                } catch (e: PackageManager.NameNotFoundException) {
                    result.success(false) // 应用未安装
                }
            } else {
                result.notImplemented()
            }
        }*/

    }


    override fun onDestroy() {
        super.onDestroy()
        // 注销广播接收器
//        unregisterReceiver(broadcastReceiver)
    }
}

